package com.dubbo.mysql;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 客户提升统计信息
 * 保存在内存变量中查看当日实时统计信息
 * 包括当日插入与更新次数
 * 
 * 
 * */



public class CustomImprove {
	
	// 通常情况下，从内存读取保存的统计信息，可随时读取。但程序崩溃后需从数据库读取统计信息后更新
	static DateFormat df=new SimpleDateFormat("yyyy/MM/dd");//设置显示格式
	static int todayinsert = 0;
	static int todayupdate = 0;
	public static String LastDate=df.format(new Date(System.currentTimeMillis()));//程序启动时设置启动日期
	public static void settodayinsert() {//对外接口，每调用一次todayinsert+1
		if(!LastDate.equals(df.format(new Date(System.currentTimeMillis()))))
			settodayinsert(0);
		todayinsert++;
	}

	public static int gettodayinsert() {//对外接口，获取todayinsert值
		return todayinsert;
	}

	public static void settodayupdate() {//对外接口，每调用一次todayupdate+1
		if(!LastDate.equals(df.format(new Date(System.currentTimeMillis()))))
			settodayupdate(0);
		todayupdate++;
	}
	
	public static int gettodayupdate() {//对外接口，获取当天更新量值
		return todayupdate;
	}
	
	private static void settodayinsert(int ti) {//对内接口  设置当天插入量值
		todayinsert = ti;
	}

	private static void settodayupdate(int ti) {//设置当天更新量值
		todayupdate = ti;
	}



	
	// 以下为异常情况下，从数据库调出数据。从数据库提取统计信息，耗时较长
	//获取指定日期的插入量,格式参考20150521
	
	public static int sqlgettodayinsert(String day)
	{
		int rowCount = 0;
		for (int i = 0; i < 32; i++) {
			String sql = String
					.format("SELECT count(*) as rowcount FROM `users_%s` WHERE TO_DAYS('%s') = TO_DAYS(`CreatTime`)",
							i, day);
			try {
				rowCount += Distrmysql.dateselect(sql);
			} catch (Exception e) {
				rowCount = -1;
				break;
			}
		}
		return rowCount;
	}

	// 读取某日log入库记录总数，即获取某日的更新量，格式参考20150521
	public static int sqlgetupdatelog(String day) {
		int rowCount = 0;
		String sql = String
				.format("SELECT count(*) as rowcount FROM `updatelog` WHERE TO_DAYS('%s') = TO_DAYS(`EditTime`)",
						day);
		try {
			rowCount += Distrmysql.dateselect(sql);
		} catch (Exception e) {
			rowCount = -1;
		}
		return rowCount;
	}
}
